package com.seafood.controller;

import com.seafood.common.Result;
import com.seafood.dto.PaymentRequestDTO;
import com.seafood.dto.PaymentResponseDTO;
import com.seafood.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 支付控制器
 */
@Tag(name = "支付管理", description = "支付相关API")
@RestController
@RequestMapping("/payment")
@RequiredArgsConstructor
@Validated
public class PaymentController {
    
    private final PaymentService paymentService;
    
    /**
     * 创建支付订单
     */
    @Operation(summary = "创建支付订单", description = "创建支付订单，返回支付链接")
    @PostMapping("/create")
    public Result<PaymentResponseDTO> createPayment(@Valid @RequestBody PaymentRequestDTO paymentRequest) {
        try {
            PaymentResponseDTO response = paymentService.createPayment(paymentRequest);
            return Result.success("支付订单创建成功", response);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 模拟支付成功回调
     */
    @Operation(summary = "模拟支付成功", description = "模拟支付成功回调，更新订单状态")
    @PostMapping("/simulate/success")
    public Result<String> simulatePaymentSuccess(@RequestParam String orderNumber) {
        try {
            paymentService.simulatePaymentSuccess(orderNumber);
            return Result.success("支付成功，订单状态已更新");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 查询支付状态
     */
    @Operation(summary = "查询支付状态", description = "查询支付订单状态")
    @GetMapping("/status")
    public Result<PaymentResponseDTO> getPaymentStatus(@RequestParam String paymentId) {
        try {
            PaymentResponseDTO response = paymentService.getPaymentStatus(paymentId);
            return Result.success(response);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}